home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 July / EnigmA AMIGA RUN 20 (1997)(G.R. Edizioni)(IT)[!][issue 1997-07 & 08][EAR-CD IV].iso / earcd / biz / dbase / ax.lha / Ax / Rexx / FTP_AmFTP.ax next >
Text File  |  1997-03-15  |  9KB  |  348 lines

  1. /*
  2.  * Ax ARexx-FTP interface script
  3.  * FTP client: AmFTP
  4.  * $VER: FTP_AmFTP.ax 1.0 (6.12.96)
  5.  *
  6.  * Version 1.0
  7.  * by Paul A. Schifferer
  8.  * Copyright 1996 © Isengard Developments
  9.  *
  10.  * This script is freely distributable and modifiable.  You are authorized to
  11.  * modify it to suit your needs.  Isengard Developments makes no warranty,
  12.  * express or implied, for its usability or reliability in its original or
  13.  * modified form.
  14.  *
  15.  * This script takes an optional list of files to retrieve with the FTP client
  16.  * program noted above.  If 'files' aren't specified on the command line, the
  17.  * script will attempt to interface with Ax, using the ARexx port name "AXFTP"
  18.  * to get files.  Alternatively, 'files' can be "LIST=filename", which
  19.  * specifies the list of files to use (see below).  If files are specified on
  20.  * the command line for this script, then the first parameter must be the site
  21.  * name, the second the base directory, and all subsequent parameters are
  22.  * files in the form of 'path/filename' (relative to the base path).
  23.  *
  24.  * The file list is in the following format:
  25.  *
  26.  * The first line is the site from which to retrieve the files.  The second
  27.  * line is the base directory to use, e.g., '/pub/aminet', without the
  28.  * trailing slash.  This dir should always be an absolute path, i.e., with
  29.  * the beginning slash.
  30.  *
  31.  * All subsequent lines are files to retrieve in the form of 'path/filename',
  32.  * e.g., "biz/dbase/Ax.lha".  'path' need not be specified, if the file is
  33.  * located in the base directory.  Note that all paths should be specified
  34.  * relative to the base directory, but this is not a requirement.  Absolute
  35.  * pathnames may be used.
  36.  *
  37.  * A line with three hashes ('###') ends the filelist.  Everything thereafter
  38.  * is ignored.
  39.  */
  40.  
  41. options results
  42. signal on syntax
  43. signal on error
  44.  
  45. parse arg files
  46.  
  47. /*
  48.  * Is Ax running?
  49.  */
  50. if (length(files) = 0) & ~show('P',"AX") then do
  51.   say "Ax must be running to use this script!"
  52.   exit 20
  53.   end
  54.  
  55. /*
  56.  * Set up some variables.
  57.  */
  58. clientname = "AmFTP"
  59. scriptname = "FTP_"clientname".ax"
  60. clientpath = "AmFTP"
  61. clientopts = ""
  62. clientport = "AMFTP.1"
  63. ftpport = ""
  64.  
  65. /*
  66.  * Command variables.
  67.  *
  68.  * The following variables are commands that would be issued to the FTP client
  69.  * program.  The construct is simple:  "command parameter options".  Place
  70.  * the client's command portion in the variable <a-cmd>cmd, where <a-cmd> is
  71.  * the type of command.  This script will place after the command any parameters
  72.  * that are necessary.  If the command has options to be specified, place them
  73.  * in the variable <a-cmd>opts.  These will be placed at the end of the command,
  74.  * after the parameters.
  75.  *
  76.  * For example, the "GET" command for DaFTP allows you to specify the transfer
  77.  * mode.  To set up this command, you would put "GET" in the variable 'getcmd',
  78.  * and "BINARY" in the variable 'getopts'.  When the command is issued, it
  79.  * will be sent to DaFTP as "GET <filename> BINARY".
  80.  *
  81.  * It's really very simple.  If you are having problems with it, just send me
  82.  * e-mail <gandalf@hughes.net>, and we'll work it out, okay?
  83.  */
  84. sitecmd = "CONNECTHOST"
  85. siteopts = ""
  86. connectcmd = ""
  87. connectopts = ""
  88. portcmd = ""
  89. portopts = ""
  90. binarycmd = ""
  91. binaryopts = ""
  92. cdcmd = "CHANGEDIR"
  93. cdopts = ""
  94. lcdcmd = "CHANGELOCALDIR"
  95. lcdopts = ""
  96. getcmd = "RECEIVE"
  97. getopts = ""
  98. quitcmd = "QUIT"
  99. quitopts = ""
  100.  
  101. /*
  102.  * Check if environment var for FTPCLIENT is set and use it.  The first line
  103.  * in the var would be the full path of the FTP client to use.  The second
  104.  * line, which is optional, specifies any options to use on the command line
  105.  * when starting the client program.
  106.  */
  107. if exists("ENV:FTPCLIENT") then do
  108.   if open('fc',"ENV:FTPCLIENT",'R') then do
  109.     line = strip(readln('fc'))
  110.     if length(line) > 0 then clientpath = line
  111.     line = strip(readln('fc'))
  112.     if length(line) > 0 then clientopts = line
  113.     end
  114.   call close('fc')
  115.   end
  116.  
  117. /*
  118.  * Start FTP client.
  119.  */
  120. if ~show('P',clientport) then do
  121.   say "Starting FTP client..."
  122.   address command "Run >NIL: "clientpath clientopts
  123.   end
  124.  
  125. /*
  126. if rc ~= 0 then do
  127.   say scriptname": Error" rc "trying to start FTP client '"clientname"'!"
  128.   exit 10
  129.   end
  130. */
  131.  
  132. /*
  133.  * Wait for ARexx port to appear (2 minute timeout).
  134.  */
  135. call time('R')
  136. do forever
  137.   if show('P',clientport) then leave
  138.   call Delay(250)
  139.   if time('E') > 120 then do
  140.     say scriptname": Timeout waiting for client port '"clientport"'!"
  141.     exit 10
  142.     end
  143.   end
  144.  
  145. /*
  146.  * Set up port #.
  147.  */
  148. address value(clientport)
  149. if length(ftpport) > 0 then do
  150.   ""portcmd ftpport portopts
  151.   end
  152.  
  153. /*
  154.  * Get site name & base dir.
  155.  */
  156. address AXFTP
  157. "Site"
  158. site = result
  159. "BaseDir"
  160. basedir = result
  161.  
  162. /*
  163.  * Connect to site.
  164.  */
  165. address value(clientport)
  166. ""sitecmd site siteopts
  167. if length(connectcmd) > 0 then do
  168.   ""connectcmd connectopts
  169.   end
  170.  
  171. if rc ~= "0" then do
  172.   say scriptname": Unable to connect to site '"site"'!"
  173.   address AX "ErrorReq" scriptname" ("rc"): Unable to connect to site '"site"'!"
  174.   address AXFTP "Status" scriptname" ("rc"): Unable to connect to site '"site"'!"
  175.   exit 10
  176.   end
  177.  
  178. /*
  179.  * Set transfer mode.
  180.  */
  181. if length(binarycmd) > 0 then do
  182.   ""binarycmd binaryopts
  183.   end
  184.  
  185. /*
  186.  * Were files or a filelist specified?
  187.  */
  188. filelist = ""
  189. if length(files) > 0 then do
  190.   if upper(substr(files,1,5)) = "LIST=" then do
  191.     filelist = word(substr(files,6),1)
  192.     c = usefilelist(filelist)
  193.     if c ~= 0 then exit c
  194.     signal thatsit
  195.     end
  196.   else do
  197.     c = usefiles(files)
  198.     if c ~= 0 then exit c
  199.     signal thatsit
  200.     end
  201.   end
  202.  
  203. /*
  204.  * The following code controls interaction with Ax (via the ARexx ports AX and
  205.  * AXFTP) and the FTP client to grab files.
  206.  */
  207.  
  208. /* tell Ax to start FTP thread, if not running */
  209. address AX
  210. "StartFTP"
  211. if result = "0" then do
  212.   say scriptname": Unable to start Ax's FTP thread!"
  213.   exit 10
  214.   end
  215. call time('R')
  216. do forever
  217.   if show('P',"AXFTP") then leave
  218.   call Delay(250)
  219.   if time('E') > 120 then do
  220.     say scriptname": Timeout waiting for port 'AXFTP'!"
  221.     exit 10
  222.     end
  223.   end
  224.  
  225. /*
  226.  * Reset file list.
  227.  */
  228. "ResetFilelist"
  229.  
  230. /*
  231.  * Main file retrieval loop.
  232.  */
  233. do forever
  234.   address AXFTP
  235.   "GetFilename"
  236.   file = result
  237.   if file = "" then leave
  238.   "GetPath ABSOLUTE"
  239.   path = result
  240.  
  241.   address AXFTP "Status Changing directory to '"path"'..."
  242.   address value(clientport) ""cdcmd path cdopts
  243.   address AXFTP "Status Receiving '"file"'..."
  244.   address value(clientport) ""getcmd file getopts
  245.  
  246.   address AXFTP
  247.   "SetFile DOWNLOADED"
  248.   "RemoveFile"
  249.   "NextFile"
  250.   end
  251.  
  252. thatsit:
  253. /*
  254.  * Tell FTP client to shut down.
  255.  */
  256. if length(quitcmd) > 0 then do
  257.   address value(clientport)
  258.   ""quitcmd quitopts
  259.   end
  260.  
  261. exit 0
  262.  
  263. /*
  264.  * This procedure lets the script grab files via the FTP client using a file
  265.  * list.
  266.  */
  267. usefilelist:
  268.   parse arg flist
  269.  
  270. error = 0
  271.  
  272. if open('fl',flist,'R') then do
  273.   site = strip(readln('fl'))
  274.   basedir = strip(readln('fl'))
  275.   do while ~eof('fl')
  276.     fpath = readln('fl')
  277.     if fpath = "###" then leave
  278.     if substr(fpath,1,1) = "/" then do /* absolute pathname */
  279.       fpath = reverse(fpath)
  280.       parse fpath fname "/" fpath
  281.       fpath = reverse(fpath)
  282.       fname = reverse(fname)
  283.       end
  284.     else if index(fpath,"/") > 0 then do /* relative pathname is specified */
  285.       fpath = reverse(basedir"/"fpath)
  286.       parse fpath fname "/" fpath
  287.       fpath = reverse(fpath)
  288.       fname = reverse(fname)
  289.       end
  290.     else do
  291.       fname = fpath
  292.       fpath = basedir
  293.       end
  294.     address value(clientport)
  295.     ""cdcmd fpath cdopts
  296.     ""getcmd fname getopts
  297.     end
  298.   call close('fl')
  299.   end
  300.  
  301. return error
  302.  
  303. /*
  304.  * This procedure lets the script grab files via the FTP client using files
  305.  * specified on the command line.
  306.  */
  307. usefiles:
  308.   parse arg site basedir flist
  309.  
  310. error = 0
  311.  
  312. do forever
  313.   parse var flist fpath flist
  314.   if fpath = "" then leave
  315.   if substr(fpath,1,1) = "/" then do /* absolute pathname */
  316.     fpath = reverse(fpath)
  317.     parse fpath fname "/" fpath
  318.     fpath = reverse(fpath)
  319.     fname = reverse(fname)
  320.     end
  321.   else if index(fpath,"/") > 0 then do /* relative pathname is specified */
  322.     fpath = reverse(basedir"/"fpath)
  323.     parse fpath fname "/" fpath
  324.     fpath = reverse(fpath)
  325.     fname = reverse(fname)
  326.     end
  327.   else do
  328.     fname = fpath
  329.     fpath = basedir
  330.     end
  331.   end
  332.   address value(clientport)
  333.   ""cdcmd fpath cdopts
  334.   ""getcmd fname getopts
  335.   end
  336.  
  337. return error
  338.  
  339. syntax:
  340. err = rc
  341. parse source . . . me .
  342. address AX "ErrorReq ARexx error" err "in line" sigl "of" me"."
  343. exit
  344.  
  345. error:
  346. parse source . . . me .
  347. address AX "ErrorReq Error" rc "in line" sigl "of" me"."
  348.